package com.sensePlusVR.fragment;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.xutils.x;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.sensePlusVR.adapter.FollowAllAdapter;
import com.sensePlusVR.adapter.FollowFollowAdapter;
import com.sensePlusVR.base.BaseFragment;
import com.sensePlusVR.bean.ActorItem;
import com.sensePlusVR.main.R;
import com.sensePlusVR.manager.ActorManager;
import com.sensePlusVR.manager.DataCenter;
import com.sensePlusVR.manager.EventListener;
import com.sensePlusVR.utils.MyLog;
import com.sensePlusVR.utils.UserUtil;
import com.sensePlusVR.widget.FullyGridLayoutManager;
import com.sensePlusVR.widget.FullyLinearLayoutManager;
@SuppressLint("NewApi")
@ContentView(R.layout.fragment_follow)
public class FollowFragment extends BaseFragment implements OnClickListener{
	private static final String TAG = FollowFragment.class.getSimpleName();
	private List<ActorItem> followFollowList;
	private List<ActorItem> followAllList;
	private DataCenter dataCenter;
	private FollowFollowAdapter followFollowAdapter;
	private FollowAllAdapter followAllAdapter;
	
	@ViewInject(R.id.follow_follow_recycler)
	private RecyclerView follow_follow_recycler;
	
	@ViewInject(R.id.follow_follow_pull_down_relative)
	private RelativeLayout follow_follow_pull_down_relative;
	
	
	@ViewInject(R.id.follow_follow_pull_down_iv)
	private ImageView follow_follow_pull_down_iv;
	
	@ViewInject(R.id.follow_all_recycler)
	private RecyclerView follow_all_recycler;
	
	@ViewInject(R.id.follow_all_pull_down_relative)
	private RelativeLayout follow_all_pull_down_relative;
	
	@ViewInject(R.id.follow_all_pull_down_iv)
	private ImageView follow_all_pull_down_iv;
	
	
	private final String KEY=this.getClass().getSimpleName();
	EventListener listenerActors = new EventListener(){

		@SuppressWarnings("unchecked")
		@Override
		public void Notify(int event, Object data, Object Params) {
			if (Params == null || data == null)
				return;
			MyLog.d(TAG, "listenerActors event : " + event);
			if (event == ActorManager.EventActorAllGot) {
				List<ActorItem> list = (List<ActorItem>) data;
				if (list != null && list.size() > 0) {
					followAllList.clear();
					followAllList.addAll(list);
					followAllAdapter.refreshData();
				}
			} else if (event == ActorManager.EventActorIdsGot) {
				List<ActorItem> list = (List<ActorItem>) data;
				if (list != null && list.size() > 0) {
					followFollowList.clear();
					followFollowList.addAll(list);
					followFollowAdapter.refreshData();
				}
			}
		}
		
	};
	
	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dataCenter = DataCenter.getInstans();
	}
	
	private void beforeView() {
		int userId = UserUtil.getUserId();
		if (followFollowList == null) {
			followFollowList = new ArrayList<ActorItem>();
		}
		followFollowList.clear();
		if (followFollowList.size() == 0){
			List<ActorItem> list = dataCenter.getFollowedActors(userId);
			if (list != null){
				followFollowList.addAll(list);
				MyLog.d(TAG, "followFollowList : " + followFollowList.toString());
				Collections.reverse(followFollowList);
				MyLog.d(TAG, "followFollowList reverse : " + followFollowList.toString());
			}
		}
		
		if (followAllList == null) {
			followAllList = new ArrayList<ActorItem>();
		}
		followAllList.clear();
		
		if (followAllList.size() == 0){
			List<ActorItem> list = dataCenter.getActors(false);
			if (list != null)
				followAllList.addAll(list);
		}
		followFollowAdapter = new FollowFollowAdapter(getActivity(),followFollowList);
		followAllAdapter = new FollowAllAdapter(getActivity(),followAllList);
		
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = x.view().inject(this, inflater, container); 
		beforeView();
		initView();
		dataCenter.addActorEventListener(listenerActors, this, KEY);
		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		if (followFollowAdapter != null) {

			followFollowAdapter.onResume();
		}

		if (followAllAdapter != null) {

			followAllAdapter.onResume();
		}
	}
	
	
	@Override
	public void onPause() {
		super.onPause();
		
		if (followFollowAdapter != null) {

			followFollowAdapter.onPause();
		}

		if (followAllAdapter != null) {

			followAllAdapter.onPause();
		}
	}

	private void initView() {
		FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(getActivity());
		linearLayoutManager.setSmoothScrollbarEnabled(true);
		linearLayoutManager.setAutoMeasureEnabled(true);
		follow_follow_recycler.setLayoutManager(linearLayoutManager);
		follow_follow_recycler.setNestedScrollingEnabled(false);
		follow_follow_recycler.setAdapter(followFollowAdapter);
		
		FullyGridLayoutManager gridLayoutManager = new FullyGridLayoutManager(getActivity(), 2);
		gridLayoutManager.setSmoothScrollbarEnabled(true);
		gridLayoutManager.setAutoMeasureEnabled(true);
		follow_all_recycler.setLayoutManager(gridLayoutManager);
		follow_all_recycler.setNestedScrollingEnabled(false);
		follow_all_recycler.setAdapter(followAllAdapter);
		
		follow_follow_pull_down_relative.setOnClickListener(this);
		follow_all_pull_down_relative.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.follow_follow_pull_down_relative:
			MyLog.d(TAG, "follow_follow_pull_down_iv...");
			if(follow_follow_recycler.getVisibility() == View.VISIBLE){
				follow_follow_recycler.setVisibility(View.GONE);
				follow_follow_pull_down_iv.setImageResource(R.drawable.list_unfold_selector);
			}else{
				follow_follow_recycler.setVisibility(View.VISIBLE);
				follow_follow_pull_down_iv.setImageResource(R.drawable.list_fold_selector);
			}
			break;
		case R.id.follow_all_pull_down_relative:
			MyLog.d(TAG, "follow_all_pull_down_iv...");
			if(follow_all_recycler.getVisibility() == View.VISIBLE){
				follow_all_recycler.setVisibility(View.GONE);
				follow_all_pull_down_iv.setImageResource(R.drawable.list_unfold_selector);
			}else{
				follow_all_recycler.setVisibility(View.VISIBLE);
				follow_all_pull_down_iv.setImageResource(R.drawable.list_fold_selector);
			}
			break;
		}
	}
	

	
	@Override
	public void onDestroyView() {
		super.onDestroyView();
		dataCenter.delActorEventListener(KEY);
	}
	
	
	

}
